<template>
  <div class="container">
    <el-card>
      <img class="logo" src="../../assets/images/logo.png" alt="" />
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules" >
  <el-form-item prop="phone">
    <el-input v-model="form.phone" placeholder="手机号"></el-input>
  </el-form-item>
   <el-form-item prop="code">
    <el-input v-model="form.code" placeholder="验证码"></el-input>
  </el-form-item>
  <el-form-item prop="agress">
      <el-checkbox label="请勾选协议" name="type" v-model="form.agress"></el-checkbox>

  </el-form-item>

  <el-form-item >
    <el-button type="primary" @click="onSubmit" style="width:100%">登录</el-button>
  </el-form-item>
</el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api'
import { setToken } from '@/utils/token'
export default {
  data () {
    var validCheck = (rule, value, callback) => {
    //   console.log(value)
      if (value) {
        callback()
      } else {
        callback(new Error('请勾选'))
      }
    }
    return {
      form: {
        phone: '15176919953',
        code: '246810',
        agress: false
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change', pattern: /^1[3-9]\d{9}$/ }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change', pattern: /^\d{6}$/ }
        ],
        agress: [
          { required: true, message: '请勾选', trigger: 'change', validator: validCheck }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const res = await login({ phone: this.form.phone, code: this.form.code })
          try {
            this.$message({
              showClose: true,
              message: '登录成功',
              type: 'success'
            })
            setToken(res.data.data.token)
            this.$router.push('/')
          } catch (err) {
            console.log('用户名或密码错误')
            console.error(err)
          }
        //   console.log('登录成功')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(../../assets/images/login.png);
  .el-card {
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.1);
    .logo {
      width: 200px;
      display: block;
      margin: 0 auto 20px;
    }
  }
}
</style>
